package com.huifer.wgx.controller;

import com.huifer.wgx.pojo.CommonResult;
import com.huifer.wgx.model.PersonnelEntity;
import com.huifer.wgx.service.PersonnelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>Title : PersonnelController </p>
 * <p>Description : 工程</p>
 *
 * @author huifer
 * @date 2019-03-15
 */
@RestController
@RequestMapping("personnel")
@Api(tags = "PersonnelController", description = "人员管理")
public class PersonnelController {

    private CommonResult commonResult = new CommonResult();

    @Autowired
    private PersonnelService personnelService;


    @ApiOperation("创建人员")
    @PostMapping("/saveone")
    public ResponseEntity savePersonnel(@RequestParam("personnel_name") String personnel_name,
            @RequestParam("function_id") Long function_id) {

        try {
            PersonnelEntity personnelEntity = personnelService.saveOne(personnel_name, function_id);
            return ResponseEntity.ok(commonResult.success(personnelEntity));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }


    @ApiOperation("创建人员")
    @PostMapping("/save")
    public ResponseEntity savePersonnel(PersonnelEntity personnel) {
        try {
            PersonnelEntity personnelEntity = personnelService.savePersonnel(personnel);
            return ResponseEntity.ok(commonResult.success(personnelEntity));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("查询所有人员")
    @GetMapping("/find_all")
    public ResponseEntity findAllPersonne(@RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize) {
        try {
            return ResponseEntity
                    .ok(commonResult.success(personnelService.findAll(pageNum, pageSize)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("修改人员名称根据id")
    @PutMapping("/edit")
    public ResponseEntity update(@RequestParam("name") String name,
            @RequestParam("funcid") Long funcid) {
        try {
            PersonnelEntity p = personnelService.updatePersonnel(name, funcid);
            return ResponseEntity.ok(commonResult.success(p));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("查询所有人员")
    @GetMapping("/list")
    public ResponseEntity list() {
        try {
            return ResponseEntity
                    .ok(commonResult.success(personnelService.list()));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

}
